package utils

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"go_payment/app/param_model"
	"sort"
	"strings"
)

func GenSign() string {
	return ""
}

func AuthSign(trade param_model.Trade, md5key string) bool {
	//组装签名字段
	signMap := make(map[string]interface{})
	signMap["pay_memberid"] = trade.MerchantId
	signMap["pay_bankcode"] = trade.BankCode
	signMap["pay_amount"] = trade.Amount
	signMap["pay_notifyurl"] = trade.NotifyUrl
	signMap["pay_callbackurl"] = trade.CallbackUrl
	signMap["pay_orderid"] = trade.OrderId
	signMap["pay_applydate"] = trade.ApplyDate

	//键值对按ASCII排列
	var keys []string
	for k, _ := range signMap {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	var str string
	for _, v := range keys {
		str += (v + "=" + fmt.Sprint(signMap[v]) + "&")
	}
	str += ("key=" + md5key)

	h := md5.New()
	h.Write([]byte(str))
	sign := hex.EncodeToString(h.Sum(nil))
	sign = strings.ToUpper(sign)

	if sign == trade.Sign {
		return true
	}
	return false
}
